const express = require('express')

const app = express();

app.listen(3006, function () {
    console.log('http://localhost:3006')
});

app.use(express.urlencoded({ extended: false }))

const db = require('./database/index')

app.post('/api/reguser', (req, res) => {
    // console.log(req.body)
    let { username, password } = req.body
    console.log(username)

    let sql = `select * from user where username="${username}"`

    db(sql, (err, results) => {
        if (err) {
            return res.send({ status: 1, msg: 'sql语法错误' + err.message });
        }

        if (results.length >= 1) {
            return res.send({ status: 1, msg: '用户名已存在,请更换用户名!' });
        }


        const utility = require('utility')
        password = utility.md5(password)
        let sql = `insert into user set username='${username}',password='${password}'`
        db(sql, (err, results) => {
            if (err) {
                return res.send({ status: 1, message: 'sql语法错误:' + err.message })
            }

            if (results.affectedRows !== 1) {
                return res.send({ status: 1, message: '添加失败!' })
            }

            res.send({ status: 0, message: '注册成功!' })
        })



    })


})
